package demo.main;

import static net.sf.dynamicreports.report.builder.DynamicReports.cht;
import static net.sf.dynamicreports.report.builder.DynamicReports.col;
import static net.sf.dynamicreports.report.builder.DynamicReports.report;
import static net.sf.dynamicreports.report.builder.DynamicReports.stl;
import static net.sf.dynamicreports.report.builder.DynamicReports.type;

import java.math.BigDecimal;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.dynamicreports.report.builder.column.TextColumnBuilder;
import net.sf.dynamicreports.report.builder.style.FontBuilder;
import net.sf.dynamicreports.report.exception.DRException;
import net.sf.jasperreports.engine.JRDataSource;

import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Ok;

public class Bar3DChart extends MutiReport {

	@At("/getBar3DChart")
	@Ok("json")
	public String getBar3DChart(HttpServletRequest request,
			HttpServletResponse response) {
		System.out.println("getBar3DChart");
		build(request, response);
		//System.out.println("dddddddd : " + resultResult);
		return resultResult;
	}

	@Override
	protected void build(HttpServletRequest request,
			HttpServletResponse response) {
		FontBuilder boldFont = stl.fontArialBold().setFontSize(12);

		TextColumnBuilder<String> itemColumn = col.column("产品", "item",
				type.stringType());
		TextColumnBuilder<Integer> quantityColumn = col.column("数量",
				"quantity", type.integerType());
		TextColumnBuilder<BigDecimal> unitPriceColumn = col.column(
				"单价", "unitprice", type.bigDecimalType());

		try {
			reportBuilder = report()
					.setTemplate(Templates.reportTemplate)
					.columns(itemColumn, quantityColumn, unitPriceColumn)
					.title(Templates.createTitleComponent(""))
					.summary(
							cht.bar3DChart()
									//.setTitle("Bar 3D chart")
									.setTitleFont(boldFont)
									.setCategory(itemColumn)
									.series(cht.serie(quantityColumn),
											cht.serie(unitPriceColumn))
									.setCategoryAxisFormat(
											cht.axisFormat().setLabel("产品")))
					.pageFooter(Templates.footerComponent)
					.setDataSource(createDataSource());
			setResult(reportBuilder.toJasperPrint(), request, response);
		} catch (DRException e) {
			e.printStackTrace();
		}
	}

	private JRDataSource createDataSource() {
		DataSource dataSource = new DataSource("item", "quantity", "unitprice");
		dataSource.add("图书", 170, new BigDecimal(100));
		dataSource.add("笔记本", 90, new BigDecimal(450));
		dataSource.add("PDA", 120, new BigDecimal(250));
		return dataSource;
	}
}
